<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>更新球队</title>
    <script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/js/bootstrap.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css"></link>
    <link rel="stylesheet" type="text/css" href="/css/style.css"></link>
    <style>
        label span {
            color: #ff0000;
            padding-right: 5px;
        }
    </style>
</head>

<body>
<h2>更新球队</h2>
<form id="myForm" class="form-horizontal">
    <!--springmvc的restful风格处理非get post方式请求必备参数-->
    <!--<input name="_method"  type="hidden" value="PUT"/>-->
    <div class="form-group">
        <label class="col-sm-1 control-label">比赛日期</label>
        <div class="col-sm-2">
            <input name="gameDate" class="form-control" type="date"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">主场球队</label>
        <div class="col-sm-2">
            <select name="homeTeamId" class="form-control">
                <option value="-1">请选择主场球队</option>
                <option value="1001">76人</option>
                <option value="1002">篮网</option>
                <option value="1003">雄鹿</option>
                <option value="1004">黄蜂</option>
                <option value="1005">老鹰</option>
                <option value="1006">热火</option>
                <option value="1007">凯尔特人</option>
                <option value="1008">尼克斯</option>
                <option value="1009">步行者</option>
                <option value="1010">公牛</option>
                <option value="1011">猛龙</option>
                <option value="1012">骑士</option>
                <option value="1013">奇才</option>
                <option value="1014">魔术</option>
                <option value="1015">活塞</option>
                <option value="1016">爵士</option>
                <option value="1017">太阳</option>
                <option value="1018">快船</option>
                <option value="1019">掘金</option>
                <option value="1020">湖人</option>
                <option value="1021">开拓者</option>
                <option value="1022">独行侠</option>
                <option value="1023">灰熊</option>
                <option value="1024">马刺</option>
                <option value="1025">勇士</option>
                <option value="1026">鹈鹕</option>
                <option value="1027">国王</option>
                <option value="1028">雷霆</option>
                <option value="1029">火箭</option>
                <option value="1030">森林狼</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"><span>*</span>主场球队分数</label>
        <div class="col-sm-9">
            <input required="required" name="homeTeamScore" class="form-control" placeholder="请输入主场球队分数"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">客场球队</label>
        <div class="col-sm-2">
            <select name="visitingTeamId" class="form-control">
                <option value="-1">请选择主场球队</option>
                <option value="1001">76人</option>
                <option value="1002">篮网</option>
                <option value="1003">雄鹿</option>
                <option value="1004">黄蜂</option>
                <option value="1005">老鹰</option>
                <option value="1006">热火</option>
                <option value="1007">凯尔特人</option>
                <option value="1008">尼克斯</option>
                <option value="1009">步行者</option>
                <option value="1010">公牛</option>
                <option value="1011">猛龙</option>
                <option value="1012">骑士</option>
                <option value="1013">奇才</option>
                <option value="1014">魔术</option>
                <option value="1015">活塞</option>
                <option value="1016">爵士</option>
                <option value="1017">太阳</option>
                <option value="1018">快船</option>
                <option value="1019">掘金</option>
                <option value="1020">湖人</option>
                <option value="1021">开拓者</option>
                <option value="1022">独行侠</option>
                <option value="1023">灰熊</option>
                <option value="1024">马刺</option>
                <option value="1025">勇士</option>
                <option value="1026">鹈鹕</option>
                <option value="1027">国王</option>
                <option value="1028">雷霆</option>
                <option value="1029">火箭</option>
                <option value="1030">森林狼</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"><span>*</span>客场球队分数</label>
        <div class="col-sm-9">
            <input required="" name="visitingTeamScore" class="form-control" placeholder="请输入客场球队分数"/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">比赛类型</label>
        <div class="col-sm-2">
            <select name="typeId" class="form-control">
                <option value="-1">请选择比赛类型</option>
                <option value="1">季前赛</option>
                <option value="2">常规赛</option>
                <option value="3">季后赛</option>
                <option value="4">总决赛</option>
                <option value="5">全明星赛</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">比赛状态</label>
        <div class="col-sm-2">
            <select name="status" class="form-control">
                <option value="0">正常</option>
                <option value="1">异常</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"></label>
        <div class="col-sm-9">
            <button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
            <button type="reset" class="btn btn-sm btn-warning">重&nbsp;置</button>
        </div>
    </div>
</form>
</body>
</html>
<script>
    String.prototype.GetValue = function (para) {
        let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    $(function () {
        //根据地址获取teamId,如果null则执行增加，否则执行更新
        let url = document.location.toString();//获取URL
        let gameId = url.GetValue("gameId");
        let pageNum = url.GetValue("pageNum");
        let pageSize = url.GetValue("pageSize");
        //alert(teamId);
        //根据id查询要更新的球队信息并回显到页面
        queryById(gameId);
        //提交按钮的单击事件--实现真正的更新
        $("#btnUpdate").click(function () {
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/game/" + gameId,
                data: $("#myForm").serialize() + "&_method=PUT",
                dataType: "json",
                success: function (vo) {
                    if (vo.code == 200) {
                        window.location.href = "/pages/game/list.html?pageNum=" + pageNum + "&pageSize=" + pageSize;
                    } else {
                        alert("更新失败！" + vo.msg);
                    }
                }
            });
        });
    });

    function queryById(gameId) {
        $.ajax({
            type: "GET",
            url: "/game/" + gameId, //RESTful风格的API定义
            data: "",
            success: function (vo) {
                let obj = vo.obj;
                let gameDate = obj.gameDate;
                let homeTeamId = obj.homeTeamId;
                let homeTeamScore = obj.homeTeamScore;
                let visitingTeamId = obj.visitingTeamId;
                let visitingTeamScore = obj.visitingTeamScore;
                let typeId = obj.typeId;
                let status = obj.status;
                $("input[name='gameDate']").val(gameDate);
                $("select[name='homeTeamId']").val(homeTeamId);
                $("input[name='homeTeamScore']").val(homeTeamScore);
                $("select[name='visitingTeamId']").val(visitingTeamId);
                $("input[name='visitingTeamScore']").val(visitingTeamScore);
                $("select[name='typeId']").val(typeId);
                $("select[name='status']").val(status);
            }
        });
    }

</script>